package doyouknow.group.CnAndZhb.controller;

import doyouknow.group.CnAndZhb.entity.*;
import doyouknow.group.CnAndZhb.response.BaseControllerReturn;
import doyouknow.group.CnAndZhb.service.*;
import doyouknow.group.CnAndZhb.utils.SerializedField;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * (TbHotelgwMap)表控制层
 *
 * @author cn
 * @since 2020-11-03 21:05:02
 */
@CrossOrigin
@RestController
@RequestMapping("tbHotelgwMap")
public class TbHotelgwMapController {
    /**
     * 服务对象
     */
    @Resource
    private TbHotelgwMapService tbHotelgwMapService;

    @Resource
    private TbSceneGwMapService tbSceneGwMapService;

    @Resource
    private TbScenePointBindService tbScenePointBindService;

    @Resource
    private TbXmmapService tbXmmapService;

    @Resource
    private TbDeviceService tbDeviceService;


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public TbHotelgwMap selectOne(Integer id) {
        return this.tbHotelgwMapService.queryById(id);
    }

    @GetMapping("information")
    @SerializedField(enable = false)
    public Object GatewayInformation(String uuid){
        TbHotelgwMap tbHotelgwMap = tbHotelgwMapService.queryByUuid(uuid);
        return BaseControllerReturn.create(tbHotelgwMap,null);
    }

    @GetMapping("ubind")
    @SerializedField(enable = false)
    public Object GatewayHotelUbind(String uuid){


        TbHotelgwMap tbHotelgwMap = tbHotelgwMapService.queryByUuid(uuid);

        if(tbHotelgwMap != null) {
            if (tbHotelgwMap.getIsDelete() == 0) {

                List<TbSceneGwMap> tbSceneGwMaps = this.tbSceneGwMapService.queryByGM(tbHotelgwMap.getGwId());
                //这里返回的是列表
                List<TbScenePointBind> tbScenePointBinds = this.tbScenePointBindService.queryByGM(tbHotelgwMap.getGwId());
                //这里返回的是列表
                TbXmmap tbXmmap = this.tbXmmapService.queryByGM(tbHotelgwMap.getGwId());
                //已完成
                List<TbDevice> tbDevices = this.tbDeviceService.queryByGM(tbHotelgwMap.getGwId());
                //这里返回的是列表
                if(tbSceneGwMaps.size() == 0 && tbScenePointBinds.size() == 0 && tbXmmap == null && tbDevices.size() == 0){

                    tbHotelgwMap.setIsDelete(1);
                    tbHotelgwMap.setUpdateTime(new Date());
                    tbHotelgwMapService.update(tbHotelgwMap);
                    return BaseControllerReturn.create(new HashMap<String,String>(){{
                        put("msg","网关解绑成功");
                    }}, null);

                }else {
                    return BaseControllerReturn.create(new HashMap<String,String>(){{
                        put("msg","请先清除网关");
                    }}, null);
                }



            } else {
                return BaseControllerReturn.create(new HashMap<String, String>() {{
                    put("msg", "网关删除过了");
                }}, null);
            }
        }else {
            return BaseControllerReturn.create(new HashMap<String, String>() {{
                put("msg", "网关不存在");
            }}, null);
        }
    }



}